﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetgameProtocol;
using Google.Protobuf.Collections;
using System.Threading;
namespace _2DgameServer.Servers
{
    class Room
    {
        private RoomPack roomInfo;//房间信息
        private Server server;

        private List<Client> ClientList = new List<Client>();//房间所有客户端
        /// <summary>
        /// 返回房间信息
        /// </summary>
        public RoomPack GetRoomInfo
        {
            //p11 1:57:32
            
            get { roomInfo.Currentnum = ClientList.Count; return roomInfo;  }
        }
        public Room(Client client,RoomPack pack,Server server)
        {
            roomInfo = pack;
            ClientList.Add(client);
            client.GetRoom = this;
            this.server = server;
        }
        //创建玩家列表
        public RepeatedField<PlayerPack> GetPlayerInFo()
        {
            RepeatedField<PlayerPack> pack = new RepeatedField<PlayerPack>();
            foreach (Client c in ClientList)
            {
                PlayerPack player = new PlayerPack();
                player.Playername = c.GetUserInFo.UserName;
                pack.Add(player);
            }
            return pack;
        }
        //广播消息
        public void Broadcast(Client client,MainPack pack)
        {
            foreach(Client c in ClientList)
            {
                if (c.Equals(client))
                {
                    continue;
                }
                c.Send(pack);
            }
        }
        public void Join(Client client)
        {
            ClientList.Add(client);
            if(roomInfo.Maxnum >= ClientList.Count)
            {
                //房间满人
                roomInfo.State = 1;
            }
            client.GetRoom = this;
            MainPack pack = new MainPack();
            pack.ActionCode = ActionCode.PlayerList;
            foreach(PlayerPack player in GetPlayerInFo())
            {
                pack.Playerpack.Add(player);
            }
            Broadcast(client, pack);
        }
        //普通成员退出
        public void Exit(Server server,Client client)
        {
            MainPack pack = new MainPack();
            if(roomInfo.State == 2)
            {
                ExitGame(client);
            }
            else
            {
                if (client == ClientList[0])
                {
                    //房主退出
                    client.GetRoom = null;
                    pack.ActionCode = ActionCode.Exit;
                    Broadcast(client, pack);
                    server.RemoveRoom(this);
                    return;
                }
                ClientList.Remove(client);
                roomInfo.State = 0;
                client.GetRoom = null;
                pack.ActionCode = ActionCode.PlayerList;
                foreach (PlayerPack player in GetPlayerInFo())
                {
                    pack.Playerpack.Add(player);
                }
                Broadcast(client, pack);
            }
            
        }
        public ReturnCode StartGame(Client client)
        {
            if(client != ClientList[0])
            {
                return ReturnCode.Field;
            }
            Thread starttime = new Thread(Time);
            starttime.Start();
            return ReturnCode.Succeed;
        }
        private void Time()
        {
            MainPack pack = new MainPack();
            pack.ActionCode = ActionCode.Chat;
            pack.Str = "房主已启动游戏.....";
            Broadcast(null, pack);
            Thread.Sleep(1000);
            for (int i = 5; i > 0; i--)
            {
                pack.Str = i.ToString();
                Broadcast(null, pack);
                Thread.Sleep(1000);
            }
            pack.ActionCode = ActionCode.Starting;
            foreach(var VARIABLE in ClientList)
            {
                PlayerPack player = new PlayerPack();
                VARIABLE.GetUserInFo.HP = 100;
                player.Playername = VARIABLE.GetUserInFo.UserName;
                player.Hp = VARIABLE.GetUserInFo.HP;
                pack.Playerpack.Add(player);
            }
            Broadcast(null,pack);
        }
        public void ExitGame(Client client)
        {
            MainPack pack = new MainPack();
            if(client == ClientList[0])
            {
                //房主退出
                pack.ActionCode = ActionCode.ExitGame;
                pack.Str = "s";
                Broadcast(client, pack);
                server.RemoveRoom(this);
                client.GetRoom = null;
            }
            else
            {
                //其它成员退出
                ClientList.Remove(client);
                client.GetRoom = null;
                pack.ActionCode = ActionCode.UpCharacterList;
                foreach(var VARIABLE in ClientList)
                {
                    PlayerPack playerPack = new PlayerPack();
                    playerPack.Playername = VARIABLE.GetUserInFo.UserName;
                    playerPack.Hp = VARIABLE.GetUserInFo.HP;
                    pack.Playerpack.Add(playerPack);
                }
                pack.Str = client.GetUserInFo.UserName;
                Broadcast(client, pack);
            }
        }

    }
}
